<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退货编号" prop="returnedOrdersId">
        <el-input
          v-model="queryParams.returnedOrdersId"
          placeholder="请输入退货单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否退款" prop="refund">
        <el-select v-model="queryParams.refund"
                show-count="true"
                placeholder="请输入是否退款"
                @keyup.enter.native="handleQuery"
                   clearable>
          <el-option label="未退款" :value="0"></el-option>
          <el-option label="已退款" :value="1"></el-option>
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.refund"-->
<!--          placeholder="请输入是否退款"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
      <el-form-item label="退货状态" prop="orderId">
        <el-select v-model="queryParams.status"
                   show-count="true"
                   placeholder="请输入是否退款"
                   @keyup.enter.native="handleQuery"
                      clearable>
          <el-option label="等待中" :value="1"></el-option>
          <el-option label="已通过" :value="2"></el-option>
          <el-option label="被驳回" :value="3"></el-option>
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.status"-->
<!--          placeholder="请输入退货单状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:orders:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退货单主键" align="center" prop="returnedOrdersId" />
      <el-table-column label="订单主键" align="center" prop="orderId" />
      <el-table-column label="退货原因" align="center" prop="reason" />
      <el-table-column label="是否退款" align="center" prop="refund" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货单状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handlePass(scope.row)"
            v-hasPermi="['system:orders:edit']"
            style="color: green"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleReject(scope.row)"
            v-hasPermi="['system:orders:remove']"
            style="color: darkred"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退货单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/system/orders";

export default {
  name: "Orders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退货单信息表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnedOrdersId: null,
        orderId: null,
        reason: null,
        refund: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单主键不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "最新更新者id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退货单信息列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.ordersList.forEach(order => {
          switch (order.status) {
            case '1':
              order.status = '等待中';
              break;
            case '2':
              order.status = '已通过';
              break;
            case '3':
              order.status = '被驳回';
              break;
            // 可以添加更多case来处理其他状态
            default:
              break;
          }
          switch (order.refund) {
            case '0':
              order.refund = '未退款';
              break;
            case '1':
              order.refund = '已退款';
              break;
            // 可以添加更多case来处理其他状态
            default:
              break;
          }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        returnedOrdersId: null,
        orderId: null,
        reason: null,
        refund: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.returnedOrdersId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退货单信息";
    },
    /** 通过按钮操作 */
    handlePass(row) {
      const returnedOrdersIds = row.returnedOrdersId || this.ids;
      this.$modal.confirm('是否确认通过退货单信息编号为"' + returnedOrdersIds + '"的退货申请？').then(function() {
        getOrders(returnedOrdersIds).then(response => {
          response.data.status=2;
          updateOrders(response.data);
        });
        return;
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("成功通过申请");
      }).catch(() => {});
    },
    /** 驳回按钮操作 */
    handleReject(row) {
      const returnedOrdersIds = row.returnedOrdersId || this.ids;
      this.$modal.confirm('是否确认通过退货单信息编号为"' + returnedOrdersIds + '"的退货申请？').then(function() {
        getOrders(returnedOrdersIds).then(response => {
          response.data.status=3;
          updateOrders(response.data);
        });
        return;
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("成功通过申请");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.status = this.form.status.join(",");
          if (this.form.returnedOrdersId != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const returnedOrdersIds = row.returnedOrdersId || this.ids;
      this.$modal.confirm('是否确认删除退货单信息编号为"' + returnedOrdersIds + '"的数据项？').then(function() {
        return delOrders(returnedOrdersIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
